package eg.com.iti.common.factory;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import eg.com.iti.common.constant.ExceptionCodes;
import eg.com.iti.common.dao.BaseDao;
import eg.com.iti.common.exception.ITISystemException;
import eg.com.iti.common.service.BaseService;



public class DaoFactory {
	private static Map<String, BaseDao> daoMap = new HashMap<String, BaseDao>();  
	public static ResourceBundle resourceBundle = ResourceBundle.getBundle("common.factory.DaoFactory");
	public static <T> T createDao(String daoName) throws ITISystemException {
		
		if (daoMap.containsKey(daoName)) {
			return (T)daoMap.get(daoName);
		}
		
		String baseDaoFullyQualifiedName = resourceBundle.getString(daoName);
		try {
			BaseDao ref = (BaseDao)Class.forName(baseDaoFullyQualifiedName).newInstance();
			daoMap.put(daoName, ref);
			return (T)ref;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			throw new ITISystemException(ExceptionCodes.INSTANTIATION_EXCEPTION);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			throw new ITISystemException(ExceptionCodes.ILLEGAL_ACCESS_EXCEPTION);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			throw new ITISystemException(ExceptionCodes.CLASS_NOT_FOUND_EXCEPTION);
		}	
	}
}
